package data;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.*;

import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * User: Vladimir
 * Date: 15.08.12 Time: 17:04
 */
public class Matrix {
    public static WritableWorkbook workbook;

    public static void main(String[] args) throws BiffException, IOException, WriteException, JAXBException, IllegalAccessException {
        File baseFolder = new File(ExtramobiNew.DATA_FOLDER);
        File[] folders = baseFolder.listFiles();
        ExtramobiNew.initItems(baseFolder, false);
        Map<String, List<ExtramobiNew.ModelItem>> modelItems = ExtramobiNew.modelItems;
        int currRow = 1;
        String currBrandName = null;
        WritableSheet sheet = null;
        for (String key : modelItems.keySet()) {
            String brandName = modelItems.get(key).get(0).brandName;
            if (currBrandName == null || !currBrandName.equals(brandName)) {
                if (sheet != null) {
                    workbook.write();
                    workbook.close();
                }
                currBrandName = brandName;
                sheet = createSheet(brandName);
                addHeader(sheet);
                currRow = 1;
             }
            addKey(sheet, key, currRow);
            List<ExtramobiNew.ModelItem> modelItemsList = modelItems.get(key);
            for (ExtramobiNew.ModelItem modelItem : modelItemsList) {
                if (modelItem.name.contains("Craftmann")) {
                    addItem(sheet, 2, currRow, "true");
                } else if (modelItem.name.contains("Аккумулятор")) {
                    addItem(sheet, 1, currRow, "true");
                } else if (modelItem.name.contains("АЗУ")) {
                    addItem(sheet, 3, currRow, "true");
                } else if (modelItem.name.contains("СЗУ")) {
                    addItem(sheet, 4, currRow, "true");
                } else if (modelItem.name.contains("Корпус")) {
                    addItem(sheet, 5, currRow, "true");
                } else if (modelItem.name.contains("Гарнитура")) {
                    addItem(sheet, 6, currRow, "true");
                } else if (modelItem.name.contains("Ремонт")) {
                    addItem(sheet, 7, currRow, "true");
                } else if (modelItem.name.contains("Тачскрин")) {
                    addItem(sheet, 8, currRow, "true");
                } else if (modelItem.name.contains("Дисплей")) {
                    addItem(sheet, 9, currRow, "true");
                } else if (modelItem.name.contains("Защитная пленка")) {
                    addItem(sheet, 10, currRow, "true");
                } else if (modelItem.name.contains("Стилус")) {
                    addItem(sheet, 11, currRow, "true");
                } else if (modelItem.name.contains("Шлейф")) {
                    addItem(sheet, 12, currRow, "true");
                } else {
                    throw new IllegalStateException("Unknown model item: " + modelItem.name);
                }
                System.out.println(key + " -> " + modelItem.name);
            }
            if (currBrandName == null || !currBrandName.equals(brandName)) {
                currBrandName = brandName;
                sheet = createSheet(brandName);
                addHeader(sheet);
            }
            currRow++;
        }
        workbook.write();
        workbook.close();

    }

    private static void addItem(WritableSheet sheet, int column, int row, String value) throws WriteException {
        WritableCell cell = new Label(column, row, value);
        sheet.addCell(cell);
    }

    private static void addKey(WritableSheet sheet, String key, int row) throws WriteException {
        WritableCell cell = new Label(0, row, key);
        sheet.addCell(cell);
    }

    public static WritableSheet createSheet(String brandName) throws IOException {
        String file = "matrix/" + brandName + ".xls";
        File xlsFile = new File(file);
        WorkbookSettings s = new WorkbookSettings();
        s.setLocale(new Locale("ru", "RU"));
        workbook = Workbook.createWorkbook(xlsFile, s);
        workbook.createSheet("Матрица", 0);
        return workbook.getSheet(0);
    }

    public static void addHeader(WritableSheet sheet) throws WriteException {
//        WritableCellFormat cf = new WritableCellFormat();
        CellView view = sheet.getColumnView(0);
        view.setAutosize(true);
        sheet.setColumnView(0, view);

        WritableCell cell = new Label(0, 0, "Модель телефона");
//        cell.setCellFormat();
        sheet.addCell(cell);
        cell = new Label(1, 0, "Аккум ориг.");
        sheet.addCell(cell);
        cell = new Label(2, 0, "Аккум Craft.");
        sheet.addCell(cell);
        cell = new Label(3, 0, "АЗУ");
        sheet.addCell(cell);
        cell = new Label(4, 0, "СЗУ");
        sheet.addCell(cell);
        cell = new Label(5, 0, "Корпус");
        sheet.addCell(cell);
        cell = new Label(6, 0, "Гарнитура");
        sheet.addCell(cell);
        cell = new Label(7, 0, "Ремонт");
        sheet.addCell(cell);
        cell = new Label(8, 0, "Тачскрин");
        sheet.addCell(cell);
        cell = new Label(9, 0, "Дисплей");
        sheet.addCell(cell);
        cell = new Label(10, 0, "Защитная пленка");
        sheet.addCell(cell);
        cell = new Label(11, 0, "Стилус");
        sheet.addCell(cell);
        cell = new Label(12, 0, "Шлейф");
        sheet.addCell(cell);
    }
}
